<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>分类管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"><!-- 确保适当的绘制和触屏缩放 -->
    <link rel="stylesheet" href="{$url_css}bootstrap.min.css" type="text/css">

    <script type="text/javascript" src="{$url_js}jquery-1.12.0.min.js"></script>
</head>
<body>
    <div class="container" style="width:96%">
        <h3>新增分类</h3>
        <table class="table table-striped table-bordered" align="center">
            <tbody>
                <tr>
                    <td>名称</td>
                    <td>分类图标URL[可不填，添加成功后再上传图标]</td>
                    <td>排序</td>
                    <td>操作</td>
                </tr>
                <tr>
                    <td class="col-xs-3"><input type="text" id="menu_name" class="form-control" placeholder="分类名称"></td>
                    <td class="col-xs-3"><input type="text" id="menu_url" class="form-control"></td>
                    <td class="col-xs-1"><input type="text" id="menu_order" class="form-control" placeholder="排序"></td>
                    <td class="col-xs-2"><button class="btn btn-primary" id="add_menu">保存</button></td>
                </tr>
            </tbody>
        </table>

        <h3>分类列表</h3>
        <table class="table table-bordered" align="center" id="list">
            <tbody>
                <tr>
                    <td></td>
                    <td>名称</td>
                    <td>分类图标URL</td>
                    <td>排序</td>
                    <td>操作</td>
                </tr>
                {foreach $cate as $v}
                    <tr class="">
                        <td class="col-xs-1">{$v.Id}</td>
                        <td class="col-xs-2"><input type="text" class="form-control menu_name" placeholder="分类名称" value="{$v.Name}"></td>
                        <td class="col-xs-4"><img src="{$v.IconUrl}" /></td>
                        <td class="col-xs-1"><input type="text" class="form-control menu_order" placeholder="排序"  value="{$v.Sort}"></td>
                        <td class="col-xs-3">
                            <button class="btn btn-default update_menu" mid="{$v.Id}">保存</button>
                            <a class="btn btn-danger delete" href="?do=DelCate&id={$v.Id}">删除</a>
                            <a class="btn btn-info" href="?do=edit&id={$v.Id}">修改【上传图片】</a>
                        </td>
                    </tr>
                {/foreach}
            </tbody>
        </table>

    </div>

    <script type="text/javascript">
        {literal}
        //添加分类
        $("#add_menu").click(function(){
            var name = $("#menu_name").val();
            var url = $("#menu_url").val();
            var order = $("#menu_order").val();
            if (name == ''){
                top.swal('出错了', '请将信息填写完整', 'error');
            }else{
                $.ajax({
                    url:'cate.php?do=AddCate',
                    type:"post",
                    data:{Name:name,Url:url,Sort:order},
                    dataType:'json',
                    success:function(re){
                        top.swal(re);
                        if(re.type == 'success'){
                            self.location.reload();
                        }
                    }
                })
            }
            
        });

        //更新分类
        $(".update_menu").click(function(){
            var obj = $(this).parent().parent();
            var name = obj.find(".menu_name").val();
            var url = obj.find(".menu_url").val();
            var order = obj.find(".menu_order").val();
            var id = $(this).attr("mid");
            if (name == ''){
                top.swal('出错了', '请将信息填写完整','','error');
            }else{
                $.ajax({
                    url:'cate.php?do=AddCate',
                    type:"post",
                    data:{Name:name,Url:url,Sort:order,Id:id},
                    dataType:'json',
                    success:function(re){
                        top.swal(re);
                        if(re.type == 'success'){
                            self.location.reload();
                        }
                    }
                })
            }
            
        });

        //删除
        $(".delete").click(function(){
            var url = self.location.pathname + $(this).attr("href");
            top.__confirm('确定要删除吗？','删除后不可恢复！', url, self);
            return false;
        })

        {/literal}
    </script>
</body>
<html>
    